#!/usr/bin/perl

use strict;
use warnings;
require 'forum_common.pl';

connect_to_database() or bad_deal();
my $user = get_current_user(get_sess_id());

record_this_hit();

print header($forum::http_header), forum_start(forum_cpanel($user));


my ($num_users, $num_pages, $page, $first, $last, $users);

$num_pages = ceil(($num_users=num_users()) / $forum::u_p);
$page = defined($page = url_param('page')) ? int($page) : 0;
$page = $num_pages if ($page > $num_pages);
$page = 1 if ($page < 1);
$first = ($page-1)*$forum::u_p + 1;
$last = ($page < $num_pages) ? $page*$forum::u_p : $num_users;

unless ($users = get_users($page))
{
	print forum_error();
	goto gameover;
}

print	"Members: ($first-$last of $num_users):", br(),
		'Pages: ';
for (1 .. $num_pages)
{
	print ' ', $_==$page ? $_ : a({-href=>"members.cgi?page=$_"}, $_);
}
print	'<div id="users">',
		div({-class=>'user'},
			div({-class=>'userc chief'}, 'Username'),
			div({-class=>'userc chief'}, 'Rank'),
			div({-class=>'userc chief'}, 'Registered on'),
			div({-class=>'userc chief'}, '# of topics'),
			div({-class=>'userc chief'}, '# of posts'));

for my $member (@$users)
{
	my $usernm = escapeHTML($member->{USERNM});
	my $dt_regist = timefmt($member->{DT_REGIST});
	my $priv = priv($member->{PRIV});
	print	'<div class="user">',
			div({-class=>'userc usernm'},
				a({-href=>"member.cgi?id=$member->{ID}"},
					$usernm)),
			div({-class=>'userc priv'}, $priv),
			div({-class=>'userc dt'}, $dt_regist),
			div({-class=>'userc ntopics'}, $member->{NUM_TOPICS}),
			div({-class=>'userc nposts'}, $member->{NUM_POSTS}),
			'</div>';
}
print '</div>';


gameover:
print forum_end();
forum_disconnect();
